/**
 * Created by wwb on 2021/1/17.
 */
import utils from "../../utils/utils.js"

window.onerror = function (err) {
    console.log('window.onerror: ' + err);
}

function isAndroid(){
	  var u = navigator.userAgent;
	  var isAndroid = u.indexOf ('Android') > - 1 || u.indexOf ('Adr') > - 1; //android终端
	  console.log(isAndroid)
	  return isAndroid
	}
function ready() {
  console.log("微信打印",window.__wxjs_environment === 'miniprogram') // true
}

function connectWebViewJavascriptBridge (callback) { 
   if (window.WebViewJavascriptBridge) {
         callback(WebViewJavascriptBridge,"建立桥连接1")
   } else {
         document.addEventListener('WebViewJavascriptBridgeReady'
              , function() {
                   callback(WebViewJavascriptBridge,"建立桥连接2")
              },
              false);
   }
}

var iFire = {};

connectWebViewJavascriptBridge (function(bridge) {
	 console.log("提供init方法",bridge);
	 try{ //此处try catch注意  android 有init初始化方法，必须调用 不调用后续注册的事件将无效 而IOS却没有init方法 调用会报错 所以得捕获异常
	   bridge.init(function(message, responseCallback) {
	     var data = {
	       'Javascript Responds': '测试中文!'
	     };
	     responseCallback(data);
	   });
	 }catch(e){
	   console.log('error',e);
	 }
})

//获取手机UUID（唯一性Id）
iFire.UUID = function (callback){
  var data = {'data': {} , 'action': 'UUID'}
  connectWebViewJavascriptBridge (function (bridge) {
    bridge.callHandler ("IFireWebViewJavascriptBridge",data , function (responseData) {
	  callback(JSON.parse(responseData))
      console.log ('接收到的UUID' , responseData);
    })
  });
}


// 打电话
iFire.OpenTel = function (phone) {
  var data = {'data': {tel:phone}, 'action': 'OpenTel'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('JS 打电话接收到的数据', responseData)
    })
  })
}

iFire.startRecord = function (callback){
  var data = {'data': {} , 'action': 'startRecord'}
  connectWebViewJavascriptBridge (function (bridge) {
    bridge.callHandler ("IFireWebViewJavascriptBridge",data , function (responseData) {
	  //callback(JSON.parse(responseData))
      console.log ('接收到的startRecord' , responseData);
    })
  });
}

iFire.scanCode = function () {
  var data = {'data': {}, 'action': 'scanCode'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('JS scanCode', responseData)
    })
  })
}

iFire.record = function (taskId) {
  var data = {'data': {taskId:taskId}, 'action': 'record'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('JS record', responseData)
    })
  })
}

iFire.back = function () {
  var data = {'data': {}, 'action': 'back'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('JS back', responseData)
    })
  })
}

iFire.AudioSwitch = function (){
	console.log("调用提醒声音");
    var data = {'data': {'time':new Date().getTime()}, 'action': 'AudioSwitch'};
    connectWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
        console.log('JS back', responseData)
      })
    })
  }
  
iFire.openAudio = function (type){
	console.log("调用提醒声音");
    var data = {'data': {'type':type}, 'action': 'openAudio'};
    connectWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
        console.log('JS closeAudio', responseData)
      })
    })
}

iFire.closeAudio = function (type){
	console.log("关闭提醒声音");
    var data = {'data': {'type':type}, 'action': 'closeAudio'};
    connectWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
        console.log('JS closeAudio1', responseData)
      })
    })
}


  iFire.test = function (){
  	console.log("调用提醒声音");
      var data = {'data': {'time':new Date().getTime()}, 'action': 'test'};
      connectWebViewJavascriptBridge(function (bridge) {
        bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
          console.log('JS back', responseData)
        })
      })
    };

iFire.getLocation = function (callback){
  var data = {'data': {} , 'action': 'getLocation'}
  connectWebViewJavascriptBridge (function (bridge) {
    bridge.callHandler ("IFireWebViewJavascriptBridge",data , function (responseData) {
	  console.log ('接收到的位置信息' , responseData);
	  callback(JSON.parse(responseData))
    })
  });
}

iFire.getMobileInfo = function (callback){
    var data = {'data': {} , 'action': 'getMobileInfo'}
    connectWebViewJavascriptBridge (function (bridge) {
        bridge.callHandler ("IFireWebViewJavascriptBridge",data , function (responseData) {
            console.log ('查询手机信息' , responseData);
            callback(JSON.parse(responseData))
        })
    });
}

iFire.bindAccount = function (account,callback){
    var data = {'data': {account:account} , 'action': 'bindAccount'}
    connectWebViewJavascriptBridge (function (bridge) {
        bridge.callHandler ("IFireWebViewJavascriptBridge",data , function (responseData) {
            console.log ('绑定账户' , responseData);
            callback(JSON.parse(responseData))
        })
    });
}

iFire.bindJgAccount = function (account,callback){
    var data = {'data': {account:account} , 'action': 'bindJgAccount'}
    connectWebViewJavascriptBridge (function (bridge) {
        bridge.callHandler ("IFireWebViewJavascriptBridge",data , function (responseData) {
            console.log ('绑定账户' , responseData);
            callback(JSON.parse(responseData))
        })
    });
}

iFire.deleteJgAccount = function (account,callback){
    var data = {'data': {account:account} , 'action': 'deleteJgAccount'}
    connectWebViewJavascriptBridge (function (bridge) {
        bridge.callHandler ("IFireWebViewJavascriptBridge",data , function (responseData) {
            console.log ('解除绑定' , responseData);
            callback(JSON.parse(responseData))
        })
    });
}

iFire.getVersion = function (callback){
    var data = {'data': {} , 'action': 'getVersion'}
    connectWebViewJavascriptBridge (function (bridge) {
        bridge.callHandler ("IFireWebViewJavascriptBridge",data , function (responseData) {
            console.log ('getVersion' , responseData);
            callback(JSON.parse(responseData))
        })
    });
}



//获取扫码的数据信息
connectWebViewJavascriptBridge(function (bridge) {
	bridge.registerHandler('getScanResult', function(data, responseCallback) {
		uni.navigateTo({
			url:'/pages/common/scanQr?type=0&ucId='+data,
		})
	})
})


iFire.getUri = function(callback){
	connectWebViewJavascriptBridge(function (bridge) {
		bridge.registerHandler('getUri', function(data, responseCallback) {
			responseCallback({'state': 'OK！',code:0})
			callback(JSON.parse(data))
		})
	})
}

iFire.acceptUrl = function(callback){
	connectWebViewJavascriptBridge(function (bridge) {
		bridge.registerHandler('acceptUrl', function(data, responseCallback) {
			responseCallback({'state': 'OK！',code:0})
			callback(data)
		})
	})
}

iFire.joinChannel = function (data) {
  var data = {'data': {'InAliRtcChannelDto':data}, 'action': 'joinChannel'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('joinChannel', responseData)
    })
  })
}

iFire.fireAlarm = function (data) {
  var data = {'data': {'InAliRtcChannelDto':data}, 'action': 'fireAlarm'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('fireAlarm', responseData)
    })
  })
}

iFire.baiduMap = function (data){
  var data = {'data': {'MapDto':data} , 'action': 'baiduMap'}
  connectWebViewJavascriptBridge (function (bridge) {
    bridge.callHandler ("IFireWebViewJavascriptBridge",data , function (responseData) {
	  console.log ('baiduMap' , responseData);
    })
  });
}

iFire.uploadFire = function (data) {
  var data = {'data': {'InAlarmRecordDto':data}, 'action': 'uploadFire'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('uploadFire', responseData)
    })
  })
}

iFire.rounds = function (data) {
  var data = {'data': {'InAlarmRecordDto':data}, 'action': 'rounds'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('rounds', responseData)
    })
  })
}

iFire.equRounds = function (data) {
  var data = {'data': {'InAlarmRecordDto':data}, 'action': 'equRounds'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('rounds', responseData)
    })
  })
}

iFire.RecordVideo = function (data) {
  var data = {'data': {'InAlarmRecordDto':data}, 'action': 'RecordVideo'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('RecordVideo', responseData)
    })
  })
}

iFire.MaintenanceRecordVideo = function (data, callback) {
  var data = {'data': {'InMaintenanceInfoDataDto':data}, 'action': 'MaintenanceRecordVideo'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      callback(data)
    })
  })
}

iFire.ExamineRecordVideo = function (data, callback) {
  var data = {'data': {'InExamineEquipment':data}, 'action': 'ExamineRecordVideo'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      callback(data)
    })
  })
}

iFire.camera = function (data) {
  var data = {'data': {'inOrderUploadDto':data}, 'action': 'camera'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('camera', responseData)
    })
  })
}

iFire.h5Camera = function () {
  var data = {'data': {}, 'action': 'h5Camera'}
  connectWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('IFireWebViewJavascriptBridge', data, function (responseData) {
      console.log('h5Camera', responseData)
    })
  })
}








export default iFire